/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-10 11:35:00
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-10 14:11:14
 * @FilePath: \goWebServer\goGameWebServer\internal\model\entity\subscribeModel.go
 * @Description: Subscribe 实体模型
 */
package entity

import (
	"goGameWebServer/internal/common/logger"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/dataType"
	"goGameWebServer/internal/interfaces"
	"goGameWebServer/internal/manager/databasemanager"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gmeta"
)

type SubscribeModelData struct {
	gmeta.Meta        `orm:"table: subscribe"`
	Id                dataType.TypeDataBaseId `orm:"id"`                // 主键
	SubscribeType     int                     `orm:"subscribeType"`     // 订阅类型
	AccountName       string                  `orm:"accountName"`       // 账号名称
	AvatarId          dataType.TypeDataBaseId `orm:"avatarId"`          // 角色ID
	ThirdPlatformType int                     `orm:"thirdPlatformType"` // 第三方类型
	UpdateTime        string                  `orm:"updateTime"`        // 更新时间
	LastSendTime      string                  `orm:"lastSendTime"`      // 最后一次推送时间
	Result            string                  `orm:"result"`            // 发送结果
	Status            int                     `orm:"status"`            // 状态
	SendTime          string                  `orm:"sendTime"`          // 发送时间
	ConditionType     int                     `orm:"conditionType"`     // 推送条件类型
	ConditionSubType  int                     `orm:"conditionSubType"`  // 推送条件子类型
	ConditionValue    int                     `orm:"conditionValue"`    // 推送条件值
}

func (d *SubscribeModelData) Reset() {
	d.Id = 0
	d.SubscribeType = 0
	d.AccountName = ""
	d.AvatarId = 0
	d.ThirdPlatformType = 0
	d.UpdateTime = ""
	d.LastSendTime = ""
	d.Result = ""
	d.Status = 0
	d.SendTime = ""
	d.ConditionType = 0
	d.ConditionSubType = 0
	d.ConditionValue = 0
}

type SubscribeModel struct {
	EntityModel
}

func (m *SubscribeModel) LoadEntityDataFromDB(records interface{}) error {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	if entityData, ok := m.EntityData.(*SubscribeModelData); ok {
		one, err := base.GetDb().Model(m.Template).Where("avatarId", entityData.AvatarId).Where("subscribeType", entityData.SubscribeType).Where("thirdPlatformType", entityData.ThirdPlatformType).Where("accountName", entityData.AccountName).One()
		if err != nil {
			return err
		}
		if one == nil {
			return m.Create()
		}
		_ = one.Struct(m.EntityData)
		m.SetEntityDBID(entityData.Id)
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "subscribe model data is invalid")
}

func (m *SubscribeModel) Create() error {
	if entityData, ok := m.EntityData.(*SubscribeModelData); ok {
		entityData.UpdateTime = utils.GetNowTimeFormat()
		entityData.LastSendTime = utils.GetNowTimeFormat()
		err := m.CreateDataToDB()
		entityData.Id = m.EntityDBID
		if err != nil {
			return err
		}
		logger.LoggerIns().Debug("create subscribe success")
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "subscribe model create fail, data is invalid")
}

func NewSubscribeModel(name string) interfaces.IEntityModel {
	m := &SubscribeModel{
		EntityModel: NewEntityModel(name),
	}
	m.SetEntityData(&SubscribeModelData{
		Id:                0,
		SubscribeType:     0,
		AccountName:       "",
		AvatarId:          0,
		ThirdPlatformType: 0,
		UpdateTime:        "",
		LastSendTime:      "",
		Result:            "",
		Status:            0,
		SendTime:          "",
		ConditionType:     0,
		ConditionSubType:  0,
		ConditionValue:    0,
	})
	m.SetTemplate(m.EntityData)
	return m
}
